package com.study.lession.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.study.lession.test.json.JsonUtil;

/**
 * 通讯录工具类
 * @author warm
 * @version 2019年07月09日
 */
public class ContactUtil {

	/**
	 * 解析request参数封装
	 * @param request
	 * @return 
	 * @return
	 */
	public static <T> T parse(HttpServletRequest request, Class<T> type) {
		Map<String, String[]> map = request.getParameterMap();
		Map<String, Object> jsonMap = new HashMap<>();
		Iterator<Map.Entry<String, String[]>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<String, String[]> entry = iterator.next();
			String[] arr = entry.getValue();
			if (null != arr && arr.length > 0) {
				StringBuffer value = new StringBuffer();
				for (String string : arr) {
					value.append(string).append(",");
				}
				String v = value.substring(0,value.length() - 1);
				jsonMap.put(entry.getKey(), v);
			}
		}

		return JsonUtil.toJavaObject(type, JsonUtil.toJson(jsonMap));
	}
	
	
}
